<!-- user表单弹窗 -->
<form id="task-form" lay-filter="task-form" class="layui-form model-form">
    <input id="taskid" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">任务范围</label>
        <div class="layui-input-block">
            <!--            <input type="radio" name="area" value="front" title="前端"/>-->
            <!--            <input type="radio" name="area" value="back" title="后台"/>-->
            <div id="treeNode"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">任务种类</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="0" title="普通任务"/>
            <input type="radio" name="type" value="1" title="紧急任务"/>
        </div>
    </div>
    <div id="time" class="layui-form-item">
        <label class="layui-form-label">截至时间</label>
        <div class="layui-input-block">
            <input id="endtim" name="endtim" type="text" class="layui-input"
                   placeholder="yyyy-MM-dd HH:mm:ss"
                   autocomplete="off">
        </div>
    </div>
    <div id="mark" class="layui-form-item">
        <label class="layui-form-label">任务积分</label>
        <div class="layui-input-block">
            <input id="integr" name="integr" placeholder="请输入任务积分" type="number" class="layui-input"
                   maxlength="20"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">任务标题</label>
        <div class="layui-input-block">
            <input id="ttitle" name="ttitle" placeholder="请输入任务标题" type="text" class="layui-input" maxlength="50"
                   autocomplete="off"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">任务内容</label>
        <div class="layui-input-block">
            <textarea id="conten" name="conten"></textarea>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="task-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'xmSelect', 'laydate', 'tinymce'], function () {
        let layer = layui.layer;
        let form = layui.form;
        let laydate = layui.laydate;
        let tinymce = layui.tinymce;
        let xmSelect = layui.xmSelect;
        let admin = layui.admin;
        let index = layui.index;
        let userinfo = '';
        let treeNode;

        index.getUser(function (user) {
            userinfo = user;
        })

        form.render('radio');


        //渲染富文本框
        tinymce.render({
            elem: "#conten",
            height: '200px'
            // 支持tinymce所有配置
        }, (opt, edit) => {
            // 加载完成后回调 opt 是传入的所有参数
            // edit是当前编辑器实例，等同于t.get返回值
        });
        let edit = tinymce.get("#conten");

        //对紧急任务输入选项的隐藏
        $("#time").hide();
        $("#mark").hide();

        var now = new Date();
        var time = now.getTime() + 1000 * 60 * 180;
        var after = new Date(time);
        //判断单选框的选取状态
        form.on('radio', function (data) {
            if (data.value == 1) {
                $("#time").show();
                $("#mark").show();
                //日期时间选择器
                laydate.render({
                    elem: '#endtim',
                    type: 'datetime',
                    value: after
                });
                $("#integr").val(6);
            } else if (data.value == 0) {
                $("#time").hide();
                $("#mark").hide();
                $("#endtim").val("");
                $ ("#integr").val("");
            }
        });

        //任务范围赋值
        admin.req("api-task/task/findTaskAreaTreeNode", {}, function (data) {
            // console.log(data);
            layer.closeAll('loading');
            if (data.code == 0) {
                treeNode = xmSelect.render({
                    el: '#treeNode',
                    filterable: true,       //搜索
                    tree: {
                        show: true,
                        expandedKeys: [],
                    },
                    height: 'auto',
                    data: data.data
                });

                let task = admin.getTempData("t_task");
                if (task) {
                    // console.log(task);
                    $("#taskid").val(task.taskid);
                    treeNode.setValue(task.tareas);
                    if (task.tktype == 0) {
                        $("#time").hide();
                        $("#mark").hide();
                        $("#endtim").val("");
                        $("input[name='type'][value='0']").prop("checked", true);
                    } else if (task.tktype == 1) {
                        $("input[name='type'][value='1']").prop("checked", true);
                        $("#time").show();
                        // $("#mark").show();
                        //日期时间选择器
                        laydate.render({
                            elem: '#endtim',
                            type: 'datetime',
                            value: task.endtim == "" ? after : task.endtim
                        });
                        // $("#integr").val(task.integr - 5);
                    }
                    $("#ttitle").val(task.ttitle);
                    $("#conten").val(task.conten);
                    edit.setContent(task.conten);
                }
            }
        }, "GET");
        form.render();


        // 表单提交事件
        form.on('submit(task-form-submit)', function (data) {
            layer.load(2);
            let taskid = $("#taskid").val();
            let userid = userinfo.id;
            let tkarea = treeNode.getValue("valueStr");
            let tktype = $("input[name='type']:checked").val();
            let endtim = $("#endtim").val();
            let integr = $("#integr").val();
            let ttitle = $("#ttitle").val();
            let conten = edit.getContent();

            let task = {
                taskid: taskid,
                userid: userid,
                tkarea: tkarea,
                tktype: tktype,
                endtim: endtim,
                integr: integr,
                ttitle: ttitle,
                conten: conten
            }
            console.log(task);
            // layer.msg(JSON.stringify(task), {icon: 1, time: 8000});
            admin.req('api-task/task/publishTaskOrUpdateTask', JSON.stringify(task), function (data) {
                layer.closeAll('loading');
                console.log(data)
                // debugger;
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, "POST");
            return false;
        });
    });
</script>